﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;
using System.Web;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     OrderInfo业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class OrderInfoBLL : BaseBLL
    {
        private static OrderInfoBLL instance = null;
        private static object syncRoot = new object();
        private Object thisLock = new Object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IOrderInfoDao dao = null;

        private OrderInfoBLL()
        {
            dao = OrderInfoSqlMapDao.Instance;
        }

        public static OrderInfoBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new OrderInfoBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                SqlMap.BeginTransaction();

                int count = dao.GetMaxCounts();

                SqlMap.CommitTransaction();

                return count;
            }
            catch
            {
                SqlMap.RollBackTransaction();

                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<OrderInfo> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<OrderInfo> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<OrderInfo> GetListByParameters(object TopCounts, OrderInfo orderinfo)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<OrderInfo> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="OrderID"></param>
        /// <returns></returns>
        public OrderInfo GetDetail(System.String orderid)
        {
            try
            {
                ///daoManager.BeginTransaction();
                return dao.GetDetail(orderid);
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        #region##添加订单
        /// <summary>
        /// 添加订单
        /// 修改人：Wilson
        /// 修改时间：2011-11-8
        /// </summary>
        /// <param name="orderinfo"></param>
        /// <returns></returns>
        public object Add(OrderInfo orderinfo)
        {
            //int id = GetId("SYS_UserInfoTemp");
            //sys_userinfotemp.UserID = id;           
            try
            {
                //SqlMap.BeginTransaction();
                lock (thisLock)
                {
                    //添加订单状态日志
                    AUV5.Model.OrderStatusLog statusLog = new OrderStatusLog();
                    statusLog.LogId = Guid.NewGuid().ToString();
                    statusLog.OrderId = orderinfo.OrderID;
                    statusLog.LogContent = AUV5.BLL.OrderStatusLogBLL.GetInstance().GetNewLogContent("", Convert.ToInt32(AUV5.Common.Enum.OrderState.Default));
                    statusLog.CreateDate = DateTime.Now;
                    statusLog.UpdateDate = DateTime.Now;

                    AUV5.BLL.OrderStatusLogBLL.GetInstance().Add(statusLog);

                    orderinfo.OrderCode = GetOrderCode();

                    object result = dao.Add(orderinfo);

                    //SqlMap.CommitTransaction();

                    return result;
                }
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                //SqlMap.RollBackTransaction();
                throw;
            }
        }
        #endregion

        #region##修改订单状态（同时修改状态日志,如果是系统自动完成或订单完成，则服务的销量统计+1）
        /// <summary>
        /// 修改订单状态（同时修改状态日志,如果是系统自动完成或订单完成，则服务的销量统计+1）
        /// 创建人：Wilson
        /// 创建时间：2011-12-4
        /// </summary>
        /// <param name="orderinfo"></param>
        /// <returns></returns>
        public int Update(OrderInfo orderinfo)
        {
            try
            {
                //SqlMap.BeginTransaction();

                //更新订单状态日志
                AUV5.Model.OrderStatusLog statusLog = AUV5.BLL.OrderStatusLogBLL.GetInstance().GetOrderStatusLogByOrderId(orderinfo.OrderID);
                statusLog.LogContent = AUV5.BLL.OrderStatusLogBLL.GetInstance().GetNewLogContent(statusLog.LogContent, orderinfo.StatusID);
                AUV5.BLL.OrderStatusLogBLL.GetInstance().Update(statusLog);

                //更新销售数量    Via  Vinlon    2011-12-27
                if (orderinfo.StatusID == (int)Common.Enum.OrderState.SysCompleted || orderinfo.StatusID == (int)Common.Enum.OrderState.Completed)
                {
                    //服务信息的销量统计+1
                    OrderItem item = OrderItemBLL.Instance.GetOrderItemByOrderID(orderinfo.OrderID);
                    ServiceInfo service = ServiceInfoBLL.Instance.GetDetail(item.ServiceID);
                    service.SalesCount += item.Quantity;
                    ServiceInfoBLL.Instance.Update(service);
                }


                //更新订单
                orderinfo.UpdateDate = DateTime.Now;

                int i = dao.Update(orderinfo);

                //SqlMap.CommitTransaction();   

                return i;
            }
            catch
            {
                //SqlMap.RollBackTransaction();
                throw;
            }
        }
        #endregion

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="OrderID"></param>
        /// <returns></returns>
        public int Delete(System.String orderid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(orderid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="orderinfo"></param>
        /// <returns></returns>
        public int Delete(OrderInfo orderinfo)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(orderinfo);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        #region##得到订单列表(分页)
        /// <summary>
        /// 得到订单列表(分页)
        /// 创建人：Wilson
        /// 创建时间：2011-10-30
        /// </summary>
        /// <param name="objParameters"></param>
        /// <returns></returns>
        public IList<OrderInfo> GetOrderListPage(NetPager pager)
        {
            try
            {
                return dao.GetOrderListPage(pager);
            }
            catch
            {

                throw;
            }
        }
        #endregion

        #region##通过订单号得到订单详情
        /// <summary>
        /// 通过订单号得到订单详情
        /// 创建人：Wilson
        /// 创建时间：2011-11-8
        /// </summary>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public OrderInfo GetOrderInfoByOrderCode(string orderCode)
        {
            try
            {
                return dao.GetOrderInfoByOrderCode(orderCode);
            }
            catch
            {

                throw;
            }
        }
        #endregion

        #region##得到订单号
        /// <summary>
        /// 得到订单号
        /// 创建人：Wilson
        /// 创建时间：2011-11-8
        /// </summary>
        /// <returns></returns>
        private string GetOrderCode()
        {
            string orderCode = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            Random rd = new Random();
            orderCode += rd.Next(0, 9999).ToString("0000");       //追加四位伪随机数
            int i = GetOrderInfoCount(orderCode);
            if (i > 0)
            {
                GetOrderCode();
            }
            return orderCode;
        }
        #endregion

        #region##通过订单号得到订单个数
        /// <summary>
        /// 通过订单号得到订单个数
        /// 创建人：Wilson
        /// 创建时间：2011-11-8
        /// </summary>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public int GetOrderInfoCount(string orderCode)
        {
            try
            {
                return dao.GetOrderInfoCount(orderCode);
            }
            catch
            {

                throw;
            }
        }
        #endregion

        #region##通过订单号更新订单
        /// <summary>
        /// 通过订单号更新订单
        /// 创建人：Wilson
        /// 创建时间：2011-11-8
        /// </summary>
        /// <param name="orderinfo"></param>
        /// <returns></returns>
        public int UpdateOrderInfoByOrderCode(OrderInfo orderinfo)
        {
            try
            {
                SqlMap.BeginTransaction();

                //更新订单状态日志
                AUV5.Model.OrderStatusLog statusLog = AUV5.BLL.OrderStatusLogBLL.GetInstance().GetOrderStatusLogByOrderId(orderinfo.OrderID);
                statusLog.LogContent = AUV5.BLL.OrderStatusLogBLL.GetInstance().GetNewLogContent(statusLog.LogContent, orderinfo.StatusID);
                AUV5.BLL.OrderStatusLogBLL.GetInstance().Update(statusLog);

                orderinfo.UpdateDate = DateTime.Now;

                int i = dao.UpdateOrderInfoByOrderCode(orderinfo);

                SqlMap.CommitTransaction();
                return i;
            }
            catch
            {
                SqlMap.RollBackTransaction();
                throw;
            }
        }
        #endregion

        #region 完成订单 Vinlon
        /// <summary>
        /// 完成订单
        /// </summary>
        /// <param name="order"></param>
        public bool CompleteOrder(OrderInfo order)
        {
            try
            {
                SqlMap.BeginTransaction();
                order.StatusID = ((int)Common.Enum.OrderState.Completed);
                Update(order);
                //服务信息的销量统计+1
                OrderItem item = OrderItemBLL.Instance.GetOrderItemByOrderID(order.OrderID);
                ServiceInfo service = ServiceInfoBLL.Instance.GetDetail(item.ServiceID);
                service.SalesCount += item.Quantity;
                ServiceInfoBLL.Instance.Update(service);
                SqlMap.CommitTransaction();
                return true;
            }
            catch
            {
                SqlMap.RollBackTransaction();
                return false;
                throw;
            }
        }
        #endregion

        #region  下单  Vinlon 2011-11-16,修改：Wilson 2011-12-4
        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="serviceid"></param>
        /// <param name="userid"></param>
        /// <param name="extralist">增值服务列表</param>
        public string OrderService(string serviceid, string userid, ArrayList extralist)
        {
            int quantity = 1;
            decimal total = 0;
            try
            {
                SqlMap.BeginTransaction();

                AUV5.Model.ServiceInfo service = AUV5.BLL.ServiceInfoBLL.Instance.GetDetail(serviceid);
                if (service != null)
                {
                    //添加订单
                    AUV5.Model.OrderInfo orderinfo = new Model.OrderInfo();
                    orderinfo.BeginDate = DateTime.Now;
                    orderinfo.Buyer = userid;
                    orderinfo.CreateDate = DateTime.Now;
                    orderinfo.EndDate = DateTime.Now;
                    orderinfo.IsDel = Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel);
                    orderinfo.OrderID = Guid.NewGuid().ToString();
                    orderinfo.Payment = 0;
                    orderinfo.Remark = "";
                    orderinfo.Seller = service.UserID;
                    orderinfo.State = Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled);
                    orderinfo.StatusID = Convert.ToInt32(AUV5.Common.Enum.OrderState.Default);
                    orderinfo.Total = 0;
                    orderinfo.UpdateDate = DateTime.Now;
                    orderinfo.Duration = service.Duration;

                    //订单Love9的数据监测
                    HttpCookie userkey = HttpContext.Current.Request.Cookies["nwr_wmas_userkey"];
                    HttpCookie cikey = HttpContext.Current.Request.Cookies["nwr_wmas_cikey"];

                    orderinfo.WMAS_UK = userkey == null ? "" : userkey.Value;
                    orderinfo.WMAS_CK = cikey == null ? "" : cikey.Value;

                    if (service.Shippable)
                    {
                        orderinfo.ShipCharge = service.NativeShipCharge;
                    }  

                    AUV5.BLL.OrderInfoBLL.Instance.Add(orderinfo);

                    //添加订单项
                    AUV5.Model.OrderItem item = new Model.OrderItem();
                    item.BeginDate = DateTime.Now;
                    item.CreateDate = DateTime.Now;
                    item.EndDate = DateTime.Now;
                    item.IsDel = Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel);
                    item.OrderID = orderinfo.OrderID;
                    item.OrderItemID = Guid.NewGuid().ToString();
                    item.Quantity = quantity;
                    item.ServiceID = service.ServiceID;
                    item.ExtraID = "";//非增值服务
                    item.State = Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled);
                    item.UpdateDate = DateTime.Now;
                    item.Price = service.Price;
                    AUV5.BLL.OrderItemBLL.Instance.Add(item);

                    total += service.Price;

                    //购买增值服务   Via Vinlon  2011-12-14
                    if (extralist != null)
                    {
                        foreach (string extraid in extralist)
                        {
                            AUV5.Model.OrderItem extraitem = new Model.OrderItem();
                            extraitem.BeginDate = DateTime.Now;
                            extraitem.CreateDate = DateTime.Now;
                            extraitem.EndDate = DateTime.Now;
                            extraitem.IsDel = Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel);
                            extraitem.OrderID = orderinfo.OrderID;
                            extraitem.OrderItemID = Guid.NewGuid().ToString();
                            extraitem.Quantity = quantity;
                            extraitem.ServiceID = service.ServiceID;
                            extraitem.ExtraID = extraid;//增值服务
                            extraitem.State = Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled);
                            extraitem.UpdateDate = DateTime.Now;
                            ExtraService extra = ExtraServiceBLL.Instance.GetDetail(extraid);
                            if (extra != null)
                            {
                                extraitem.Price = extra.Price;
                            }
                            else
                            {
                                break;
                            }
                            AUV5.BLL.OrderItemBLL.Instance.Add(extraitem);

                            //订单总价
                            total += extra.Price;
                        }
                    }

                    //更新订单总价
                    orderinfo.Total = total;
                    dao.Update(orderinfo);
                     
                    SqlMap.CommitTransaction();

                    return "/Pages/OrderInfo.aspx?OrderID=" + orderinfo.OrderID;
                }
                else
                {
                    SqlMap.RollBackTransaction();
                    throw new Exception("您购买的服务不存在！");
                }
            }
            catch
            {
                throw;
            }
        }
        #endregion

        #region##通过用户ID得到用户收入
        /// <summary>
        /// 通过用户ID得到用户收入
        /// 创建人：Wilson        
        /// 创建时间：2011-11-25
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public decimal GetIncomeByUserId(string userId)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Seller", userId);
                ht.Add("CompleteState", Convert.ToInt32(AUV5.Common.Enum.OrderState.Completed).ToString());
                ht.Add("ClosedStatus", Convert.ToInt32(AUV5.Common.Enum.OrderState.Cloesd).ToString());
                ht.Add("State", Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled));
                ht.Add("IsDel", Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel));
                return dao.GetIncomeByUserId(ht);
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region## 通过用户ID得到用户支出
        /// <summary>
        /// 通过用户ID得到用户支出
        /// 创建人：Wilson
        /// 创建时间：2011-11-25
        /// </summary>
        /// <param name="objParameters"></param>
        /// <returns></returns>
        private decimal GetCostByUserId(string userId, bool? isLocalPayment)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Buyer", userId);
                ht.Add("PaymentState", Convert.ToInt32(AUV5.Common.Enum.OrderState.Payment).ToString());
                ht.Add("ClosedStatus", Convert.ToInt32(AUV5.Common.Enum.OrderState.Cloesd).ToString());
                ht.Add("State", Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled));
                ht.Add("IsDel", Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel));
                ht.Add("IsLocalPayment", isLocalPayment);
                return dao.GetCostByUserId(ht);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过卖家ID得到强制取消订单的钱数
        /// 创建人：Wilson
        /// 创建时间：2011-12-27
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public decimal GetForceClosedByUserId(string userId)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Buyer", userId);
                ht.Add("StartStatus", Convert.ToInt32(AUV5.Common.Enum.OrderState.BuyerForceClosed).ToString());
                ht.Add("EndStatus", Convert.ToInt32(AUV5.Common.Enum.OrderState.SellerForceClosed).ToString());
                ht.Add("State", Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled));
                ht.Add("IsDel", Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel));
                return dao.GetForceOrderByParameters(ht);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 通地条件得到关闭订单应退回的价格
        /// 创建人：Wilson
        /// 创建时间：2011-12-28
        /// </summary>
        /// <param name="objParameters"></param>
        /// <returns></returns>
        public decimal GetClosedPayment(string userId)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Buyer", userId);
                ht.Add("ClosedStatus", Convert.ToInt32(AUV5.Common.Enum.OrderState.Cloesd).ToString());
                ht.Add("State", Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled));
                ht.Add("IsDel", Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel));
                return dao.GetClosedPayment(ht);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 通过用户ID得到用户总支出金额
        /// 创建人：Wilson
        /// 创建时间：2011-11-25
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public decimal GetTotalCostByUserId(string userId)
        {
            try
            {
                return GetCostByUserId(userId, null);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 通过用户ID得到用户本地余额支出金额
        /// 创建人：Wilson
        /// 创建时间：2011-11-25
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public decimal GetLocalCostByUserId(string userId)
        {
            try
            {
                return GetCostByUserId(userId, true);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 通过用户ID得到用户在线支出金额
        /// 创建人：Wilson
        /// 创建时间：2011-11-25
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public decimal GetOnlineCostByUserId(string userId)
        {
            try
            {
                return GetCostByUserId(userId, false);
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region##通过用户ID得到用户未付款
        /// <summary>
        /// 通过用户ID得到用户未付款
        /// 创建人：Wilson
        /// 创建时间：2011-11-25
        /// </summary>
        /// <param name="objParameters"></param>
        /// <returns></returns>
        public decimal GetNotPaymentByUserId(string userId)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Buyer", userId);
                ht.Add("StatusID", Convert.ToInt32(AUV5.Common.Enum.OrderState.Default).ToString());
                ht.Add("State", Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled));
                ht.Add("IsDel", Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel));
                return dao.GetNotPaymentByUserId(ht);
            }
            catch
            {
                throw;
            }
        }
        #endregion

        #region##通过用户ID得到可用余额
        /// <summary>
        /// 通过用户ID得到可用余额
        /// 创建人：Wilson
        /// 创建时间：2011-11-27
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public decimal GetBalanceByUserId(string userId)
        {
            try
            {
                return GetIncomeByUserId(userId) - GetLocalCostByUserId(userId) + GetForceClosedByUserId(userId) + GetClosedPayment(userId) - AUV5.BLL.WithDrawLogBLL.GetInstance().GetTotalAmountByUserId(userId);
            }
            catch
            {
                throw;
            }
        }
        #endregion

        #region##得到关闭的订单数
        /// <summary>
        /// 得到关闭的订单数
        /// 创建人：Wilson
        /// 创建时间：2011-12-18
        /// </summary>
        /// <param name="buyer"></param>
        /// <param name="seller"></param>
        /// <returns></returns>
        private int GetClosedOrderCount(string buyer, string seller)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Buyer", buyer);
                ht.Add("Seller", seller);
                ht.Add("CompleteState", Convert.ToInt32(AUV5.Common.Enum.OrderState.Cloesd).ToString());
                ht.Add("State", Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled));
                ht.Add("IsDel", Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel));
                return dao.GetCompletedByParameters(ht);
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 得到关闭订单数（卖家）
        /// 创建人：Wilson
        /// 创建时间：2011-12-8
        /// </summary>
        /// <param name="seller"></param>
        /// <returns></returns>
        public int GetClosedOrderCountBySeller(string seller)
        {
            try
            {
                return GetClosedOrderCount(null, seller);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 得到关闭订单数（买家）
        /// 创建人：Wilson
        /// 创建时间：2011-12-8
        /// </summary>
        /// <param name="buyer"></param>
        /// <returns></returns>
        public int GetClosedOrderCountByBuyer(string buyer)
        {
            try
            {
                return GetClosedOrderCount(buyer, null);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region##通过条件得到买/卖成功的服务数
        /// <summary>
        /// 通过条件得到买/卖成功的服务数
        /// 创建人：Wilson
        /// 创建时间：2011-11-25 
        /// </summary>
        /// <param name="buyer"></param>
        /// <param name="seller"></param>
        /// <returns></returns>
        private int GetCompleted(string buyer, string seller)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("Buyer", buyer);
                ht.Add("Seller", seller);
                ht.Add("CompleteState", Convert.ToInt32(AUV5.Common.Enum.OrderState.Completed).ToString());
                ht.Add("ClosedStatus", Convert.ToInt32(AUV5.Common.Enum.OrderState.Cloesd).ToString());
                ht.Add("State", Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled));
                ht.Add("IsDel", Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel));
                return dao.GetCompletedByParameters(ht);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 通过卖家ID得到卖成功订单数
        /// 创建人：Wilson
        /// 创建时间：2011-11-25  
        /// </summary>
        /// <param name="buyer"></param>
        /// <returns></returns>
        public int GetSellerCompleteBySeller(string seller)
        {
            try
            {
                return GetCompleted(null, seller);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 通过买家ID得到买成功订单数
        /// 创建人：Wilson
        /// 创建时间：2011-11-25 
        /// </summary>
        /// <param name="buyer"></param>
        /// <returns></returns>
        public int GetBuyerCompleteByBuyer(string buyer)
        {
            try
            {
                return GetCompleted(buyer, null);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region##通过条件得订单数量
        /// <summary>
        /// 通过条件得订单数量
        /// 创建人：Wilson
        /// 创建时间：2011-11-27
        /// </summary>
        /// <param name="buyer"></param>
        /// <param name="seller"></param>
        /// <param name="statusID"></param>
        private int GetOrderInfoCountByCondition(string buyer, string seller, string statusID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("Buyer", buyer);
            ht.Add("Seller", seller);
            ht.Add("StatusID", statusID);
            ht.Add("State", Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled));
            ht.Add("IsDel", Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel));
            return dao.GetOrderInfoCountByParameters(ht);
        }

        /// <summary>
        /// 得到强制关闭的订单数（作为卖家）
        /// 创建人：Wilson
        /// 创建时间：2011-12-23
        /// </summary>
        /// <param name="seller"></param>
        /// <returns></returns>
        public int GetForceClosedOrderInfoCountBySeller(string seller)
        {
            try
            {
                return GetOrderInfoCountByCondition(null, seller, Convert.ToInt32(AUV5.Common.Enum.OrderState.SellerForceClosed).ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 得到未付款订单数（作为卖家）
        /// 创建人：Wilson
        /// 创建时间：2011-11-27
        /// </summary>
        /// <param name="seller"></param>
        /// <returns></returns>
        public int GetDefaultOrderInfoCountBySeller(string seller)
        {
            try
            {
                return GetOrderInfoCountByCondition(null, seller, Convert.ToInt32(AUV5.Common.Enum.OrderState.Default).ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 得到未付款订单数（作为买家）
        /// 创建人：Wilson
        /// 创建时间：2011-11-27 
        /// </summary>
        /// <param name="buyer"></param>
        /// <returns></returns>
        public int GetDefaultOrderInfoCountByBuyer(string buyer)
        {
            try
            {
                return GetOrderInfoCountByCondition(buyer, null, Convert.ToInt32(AUV5.Common.Enum.OrderState.Default).ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 得到已付款订单数（作为卖家）
        /// 创建人：Wilson
        /// 创建时间：2011-11-27
        /// </summary>
        /// <param name="seller"></param>
        /// <returns></returns>
        public int GetPaymentOrderInfoCountBySeller(string seller)
        {
            try
            {
                return GetOrderInfoCountByCondition(null, seller, Convert.ToInt32(AUV5.Common.Enum.OrderState.Payment).ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 得到已付款订单数（作为买家）
        /// 创建人：Wilson
        /// 创建时间：2011-11-27 
        /// </summary>
        /// <param name="buyer"></param>
        /// <returns></returns>
        public int GetPaymentOrderInfoCountByBuyer(string buyer)
        {
            try
            {
                return GetOrderInfoCountByCondition(buyer, null, Convert.ToInt32(AUV5.Common.Enum.OrderState.Payment).ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 得到已发货订单数（作为卖家）
        /// 创建人：Wilson
        /// 创建时间：2011-11-27
        /// </summary>
        /// <param name="seller"></param>
        /// <returns></returns>
        public int GetShippedOrderInfoCountBySeller(string seller)
        {
            try
            {
                return GetOrderInfoCountByCondition(null, seller, Convert.ToInt32(AUV5.Common.Enum.OrderState.Shipped).ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 得到已发货订单数（作为买家）
        /// 创建人：Wilson
        /// 创建时间：2011-11-27 
        /// </summary>
        /// <param name="buyer"></param>
        /// <returns></returns>
        public int GetShippedOrderInfoCountByBuyer(string buyer)
        {
            try
            {
                return GetOrderInfoCountByCondition(buyer, null, Convert.ToInt32(AUV5.Common.Enum.OrderState.Shipped).ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region##得到当前订单的结束时间
        /// <summary>
        /// 当前订单之前的订单结束所需要的天数
        /// 创建人：Wilson
        /// 创建时间：2011-12-1
        /// </summary>
        /// <param name="serivceID"></param>
        /// <param name="OrderCode"></param>
        /// <returns></returns>
        private int GetEndTimeDaysByOrderCode(string serviceID, string orderCode)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("ServiceID", serviceID);
                ht.Add("OrderCode", orderCode);
                ht.Add("StatusID", Convert.ToInt32(AUV5.Common.Enum.OrderState.Payment));

                int days = dao.GetEndTimeByParameters(ht);

                AUV5.Model.OrderInfo orderinfo = AUV5.BLL.OrderInfoBLL.Instance.GetOrderInfoByOrderCode(orderCode);

                days += orderinfo.Duration;

                return days;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 得到当前订单的结束时间
        /// 创建人：Wilson
        /// 创建时间：2011-12-1
        /// </summary>
        /// <param name="serviceID"></param>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public DateTime GetEndTimeByOrderCode(string serviceID, string orderCode)
        {
            try
            {
                int day = GetEndTimeDaysByOrderCode(serviceID, orderCode);
                return DateTime.Now.AddDays(day);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 得到当前订单的结束时间
        /// 创建人：Wilson
        /// 创建时间：2011-12-1
        /// </summary>      
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public DateTime GetEndTimeByOrderCode(string orderCode)
        {
            try
            {
                AUV5.Model.OrderInfo orderInfo = GetOrderInfoByOrderCode(orderCode);

                if (orderInfo != null)
                {
                    AUV5.Model.OrderItem item = AUV5.BLL.OrderItemBLL.Instance.GetOrderItemByOrderID(orderInfo.OrderID);

                    if (item != null)
                    {
                        int day = GetEndTimeDaysByOrderCode(item.ServiceID, orderCode);
                        return DateTime.Now.AddDays(day);
                    }
                    else
                    {
                        throw new Exception("订单项不存在");
                    }
                }
                else
                {
                    throw new Exception("订单不存在");
                }


            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        /// <summary>
        /// 通过服务ID和订单状态得到订单集合
        /// 创建人：Wilson
        /// 创建时间：2011-12-26
        /// </summary>
        /// <param name="objParameters"></param>
        /// <returns></returns>
        public IList<OrderInfo> GetOrderInfoByServiceIdAndStatusId(string serviceId, int statusId)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("ServiceID", serviceId);
                ht.Add("StatusID", statusId);
                return dao.GetOrderInfoByServiceIdAndStatusId(ht);
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
